package com.techme.jetpack.navigation

import android.net.Uri
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.navigation.NavDeepLinkRequest
import androidx.navigation.NavOptions
import androidx.navigation.fragment.findNavController
import com.example.jetpack_android_online.R
import com.example.jetpack_android_online.databinding.LayoutFragmentHomeBinding


class HomeFragment : BaseFragment() {
    lateinit var homeBinding: LayoutFragmentHomeBinding
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        homeBinding = LayoutFragmentHomeBinding.inflate(inflater, container, false)
        return homeBinding.root
    }

    var flag = true
    override fun onViewCreated(view: View, savedInstanceState: Bundle?)
    {
        super.onViewCreated(view, savedInstanceState)
        val navController = findNavController()
        homeBinding.navigateToCategoryFragment.setOnClickListener{
            //演示deeplink
            navController.navigate(NavDeepLinkRequest.Builder.fromUri(Uri.parse("https://com.techme.jetpack/user?phone=123456")).build())

            // ------
//            navController.navigate(R.id.category_fragment)
            // 演示navigateUp
//            navController.navigate(R.id.category_fragment, null,
//                NavOptions.Builder().setPopUpTo(R.id.home_fragment,inclusive = true,saveState = true).build())
//            if(flag)
//            {
//                navController.navigate(R.id.category_fragment)
//                flag=false
//            } else {
//                navController.navigate(R.id.category_fragment, null, NavOptions.Builder().setRestoreState(true).build())
//            }

        }

        homeBinding.navigateUp.setOnClickListener{
//            navController.navigate(R.id.category_fragment, null, NavOptions.Builder().setRestoreState(true).build())
        // 演示clearBackStack
            navController.clearBackStack(R.id.category_fragment)

        }
    }
}